package com.meizhao.api;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.annotation.PostConstruct;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.TimeZone;


@Slf4j
@SpringBootApplication(scanBasePackages = {"com.meizhao.api.**"})
@ServletComponentScan({"com.meizhao.api.filter"})
@EnableTransactionManagement()
@EnableAspectJAutoProxy(exposeProxy = true, proxyTargetClass = true)
public class App extends SpringBootServletInitializer {


	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
		
		return builder.sources(App.class);
	}
	
	@PostConstruct
	void setTimeZone() {
		TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
	}
	
	public static void main(String[] args) throws UnknownHostException {

        SpringApplication app = new SpringApplication(App.class);

        ConfigurableApplicationContext ctx = app.run(args);
        ctx.addApplicationListener((ApplicationListener<ApplicationFailedEvent>) event
                -> {
            event.getException().printStackTrace();
        });

		Environment env = ctx.getEnvironment();
		String ip = InetAddress.getLocalHost().getHostAddress();
		String port = env.getProperty("server.port");
		String path = env.getProperty("server.servlet.context-path");
		log.info("\n----------------------------------------------------------\n\t"
				+ path.substring(1,path.length())+" is running!\n\t"
				+ "Local: \t\thttp://localhost:" + port + path
				+ "\n\t" + "External: \thttp://" + ip + ":" + port + path
				+ "\n\t" + "swagger-url: \thttp://" + ip + ":" + port + path + "/doc.html\n\t"
				+ "----------------------------------------------------------");

    }

}
